package com.example.cardandswitch.card;

import android.os.Bundle;
import android.view.View;

import com.example.cardandswitch.R;
import com.google.android.material.card.MaterialCardView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

/***
 * https://www.jianshu.com/p/d9c81521998e
 * https://blog.csdn.net/seven_7_moon/article/details/101057979
 * https://www.jianshu.com/p/b0b54537ac44
 */
public class MaterialCardActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_material_card);
        final MaterialCardView card = findViewById(R.id.mcard);
        card.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                card.setChecked(false);
                return true;
            }
        });

        card.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!card.isChecked())
                    card.setChecked(true);
                else
                    card.setChecked(false);
            }
        });

        //void setCheckable (boolean checkable)//设置是否可以选择
        //void setChecked (boolean checked)//选中的状态
        //void setCheckedIcon (Drawable checkedIcon)//选中图标的样式
        //void setCheckedIconResource (int id)//选中图标的样式
        //void setDragged (boolean dragged)//是否可拖动
        //void setRippleColor (ColorStateList rippleColor)//波纹效果的颜色
        //void setStrokeColor (int strokeColor)//卡片边的颜色
        //void setStrokeWidth (int strokeWidth)//卡片边的宽带
        //void toggle ()//控制checkable的开关

        final MaterialCardView card1 = findViewById(R.id.mcard1);
        card1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                card1.toggle();
            }
        });

        //首先，我们需要将父容器更改为DraggableCoordinatorLayout：
        final MaterialCardView card2 = findViewById(R.id.mcard2);
        //然后，我们可以添加回调并切换拖动状态：
        DraggableCoordinatorLayout dragged = findViewById(R.id.parentContainer);
        dragged.addDraggableChild(card2);
        dragged.setViewDragListener(new DraggableCoordinatorLayout.ViewDragListener() {
            @Override
            public void onViewCaptured(@NonNull View view, int i) {
                card2.setDragged(true);
            }

            @Override
            public void onViewReleased(@NonNull View view, float v, float v1) {
                card2.setDragged(false);
            }
        });
    }
}